﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
using System.Data.Objects;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblBrand_ProdCatDAL:ItblBrand_ProdCat
    {
        HKEA_CMSEntities model;
        tblBrand_ProdCat brand;

        public tblBrand_ProdCatDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblBrand_ProdCatDAL GetInstance()
        {
            return new tblBrand_ProdCatDAL();
        }

        public int InsertENT(tblBrand_ProdCat ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblBrand_ProdCat.AddObject(ent);
            return model.SaveChanges();
        }
        public int SaveList(List<int> toAddList, List<int> toDelList, int brandUID, string LastModifyBy)
        {
            foreach (var temp in toAddList)
            {
                var tempValue = model.tblBrand_ProdCat.Where(a => a.BrandUID == brandUID).
                    Where(a => a.ProductCatUID == temp).FirstOrDefault();
                if (tempValue == null)
                {
                    var ent = new tblBrand_ProdCat();
                    ent.ProductCatUID = temp;
                    ent.BrandUID = brandUID;
                    ent.CreationDate = DateTime.Now;
                    ent.Deleted = 0;
                    ent.LastModifyBy = LastModifyBy;
                    ent.LastModifyDate = DateTime.Now;
                    model.tblBrand_ProdCat.AddObject(ent);
                }
                else
                {
                    if (tempValue.Deleted == 1)
                    {
                        tempValue.Deleted = 0;
                        tempValue.LastModifyBy = LastModifyBy;
                        tempValue.LastModifyDate = DateTime.Now;
                    }
                }
            }
            foreach (var temp in toDelList)
            {
                var result = model.tblBrand_ProdCat.Where(a=>a.BrandUID==brandUID).Where(a=>a.ProductCatUID==temp).Where(a=>a.Deleted==0).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.Deleted = 1;
                }
            }
            return model.SaveChanges();
        }
        public List<tblBrand_ProdCat> GetListByENT(tblBrand_ProdCat ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblBrand_ProdCat.Where(a => a.BrandProdCatUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.BrandProdCatUID > 0)
                result = result.Where(a => a.BrandProdCatUID == ent.BrandProdCatUID);
            if (ent.BrandUID > 0)
                result = result.Where(a=>a.BrandUID==ent.BrandUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblBrand_ProdCat> GetListByENT(tblBrand_ProdCat ent)
        {
            var result = model.tblBrand_ProdCat.Where(a => a.BrandProdCatUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.BrandProdCatUID > 0)
                result = result.Where(a => a.BrandProdCatUID == ent.BrandProdCatUID);
            if (ent.BrandUID > 0)
                result = result.Where(a => a.BrandUID == ent.BrandUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public List<mtblProductSubCat> GetSubCatListByBrandUID(int uid)
        {
            var result = from Brand_subCat in model.tblBrand_ProdCat
                         join SubCat in model.mtblProductSubCats
                         on Brand_subCat.ProductCatUID equals SubCat.ProductSubCatUID
                         where Brand_subCat.Deleted == 0 && SubCat.Deleted == 0 && Brand_subCat.BrandUID == uid
                         select SubCat;
            //string str= (result as ObjectQuery).ToTraceString();
            return result.ToList();
        }
        public List<mtblProductSubCat> GetSubCatListByBrandUID(int uid, List<int> withOutList)
        {
            var result = from Brand_subCat in model.tblBrand_ProdCat
                         join SubCat in model.mtblProductSubCats
                         on Brand_subCat.ProductCatUID equals SubCat.ProductSubCatUID
                         where  Brand_subCat.Deleted == 0 && SubCat.Deleted == 0 && Brand_subCat.BrandUID == uid
                         select SubCat ;
            if (withOutList != null && withOutList.Count > 0)
                result = result.Where(a=>!withOutList.Contains(a.ProductSubCatUID));
            return result.ToList();
        }

        public int GetCountByENT(tblBrand_ProdCat ent)
        {
            var result = model.tblBrand_ProdCat.Where(a => a.BrandProdCatUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.BrandProdCatUID > 0)
                result = result.Where(a => a.BrandProdCatUID == ent.BrandProdCatUID);
            if (ent.BrandUID > 0)
                result = result.Where(a => a.BrandUID == ent.BrandUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblBrand_ProdCat ent)
        {
            brand = model.tblBrand_ProdCat.Where(a => a.BrandProdCatUID == ent.BrandProdCatUID).FirstOrDefault();
            if (brand != null)
            {
                brand.Deleted = ent.Deleted;
                brand.LastModifyBy = ent.LastModifyBy;
                brand.LastModifyDate = DateTime.Now;
                brand.ProductCatUID = ent.ProductCatUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblBrand_ProdCat ent)
        {
            var temp = model.tblBrand_ProdCat.Where(a => a.BrandProdCatUID > 0);
            if (ent.BrandProdCatUID > 0)
                temp = temp.Where(a => a.BrandProdCatUID == ent.BrandProdCatUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblBrand_ProdCat.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
        public List<tblBrand_ProdCat> GetList(tblBrand_ProdCat ent, List<int> withOutList)
        {
            var result = model.tblBrand_ProdCat.Where(a => a.BrandProdCatUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.BrandUID > 0)
                result = result.Where(a => a.BrandUID == ent.BrandUID);
            if (withOutList.Count > 0)
                result = result.Where(a => !withOutList.Contains(a.BrandProdCatUID));
            return result.ToList();
        }
    }
}
